<template>
  <j-modal
    :title="description"
    :width="'900px'"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-card :bordered="false">
      <!-- 查询区域 -->
      <div class="table-page-search-wrapper" v-if="!existProjectInfo">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="项目编号">
                <a-input placeholder="请输入项目编号" v-model="queryParam.projectCode"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="项目名称">
                <a-input placeholder="请输入项目名称" v-model="queryParam.projectName"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
                <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 查询区域-END -->
      <!-- table区域-begin -->
      <div>
        <template v-if="!existProjectInfo">
          <a-table
            ref="table"
            size="middle"
            :scroll="{ x: true }"
            bordered
            rowKey="id"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="ipagination"
            :loading="loading"
            class="j-table-force-nowrap"
            :rowSelection="{
              selectedRowKeys,
              onChange: onSelectChange,
              getCheckboxProps: chargeDetailCheckBox,
              type: 'radio',
            }"
            @change="handleTableChange"
          >
          </a-table>
        </template>

        <a-table
          :columns="innerColumns"
          :dataSource="innerData"
          :pagination="false"
          :rowSelection="{
            sectionSelectedRowKeys,
            onChange: onSectionSelectChange,
            getCheckboxProps: chargeDetailCheckBox,
            type: 'radio',
          }"
          rowKey="bidSectionCode"
          @change="handleTableChange"
        >
        </a-table>
      </div>
    </a-card>
  </j-modal>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { getAction } from '@/api/manage'
const innerColumns = [
  {
    key: 'bidSectionCode',
    title: '标段（包）编号',
    align: 'center',
    dataIndex: 'bidSectionCode',
  },
  {
    title: '标段（包）名称',
    align: 'center',
    dataIndex: 'bidSectionName',
  },
]
const innerData = []
export default {
  name: 'gcOrgSelectList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {},
  props: { projectId: {} },
  data() {
    return {
      innerColumns,
      innerData,
      /* 排序参数 */
      isorter: {
        column: 'id',
        order: 'desc',
      },
      description: '选择标段',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '项目编号',
          align: 'center',
          dataIndex: 'projectCode',
        },
        {
          title: '项目名称',
          align: 'center',
          dataIndex: 'projectName',
        },
        {
          title: '项目所在行政区域',
          align: 'center',
          dataIndex: 'regionCode_dictText',
        },
        {
          title: '投资项目统一代码',
          align: 'center',
          dataIndex: 'investProjectCode',
        },
        {
          title: '项目地址',
          align: 'center',
          dataIndex: 'address',
        },
        {
          title: '项目法人',
          align: 'center',
          dataIndex: 'legalPerson',
        },
        {
          title: '项目行业分类',
          align: 'center',
          dataIndex: 'industriesType_dictText',
        },
        {
          title: '资金来源',
          align: 'center',
          dataIndex: 'fundSource',
        },
        {
          title: '出资比例',
          align: 'center',
          dataIndex: 'contributionScale',
        },
        {
          title: '项目规模',
          align: 'center',
          dataIndex: 'projectScale',
        },
        {
          title: '是否立项',
          align: 'center',
          dataIndex: 'islixiang_dictText',
        },
        {
          title: '状态',
          align: 'center',
          dataIndex: 'status_dictText',
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          sorter: true,
        },
      ],
      url: {
        list: '/exProject/exProject/list',
        sectionList: '/exProject/exProject/queryExSectionByMainId',
        queryProjectDetail: '/exProject/exProject/queryById',
      },
      dictOptions: {},
      superFieldList: [],
      visible: false,
      disabledFormulaIds: [], //禁用不能选择的算法id
      sectionSelectedRowKeys: [],
      sectionSelectionRows: [],
      projectInfo: {},
      existProjectInfo: false, //是否 项目明细进入
    }
  },
  watch: {
    projectId: {
      immediate: true,
      handler(val) {
        if (val != undefined) {
          this.getProjectDetail()
        }
      },
    },
  },
  created() {},
  computed: {},
  methods: {
    getProjectDetail() {
      var that = this
      that.sectionSelectedRowKeys = []
      that.sectionSelectionRows = []
      that.innerData = []
      that.existProjectInfo = true

      getAction(this.url.queryProjectDetail, { id: this.projectId }).then((res) => {
        if (res.success) {
          that.projectInfo = res.result
          that.loadSectionList(that.projectId)
        }
      })
    },
    // 控制选项禁用 多个状态这样写
    chargeDetailCheckBox(record) {
      var that = this
      return {
        props: {
          disabled:
            that.disabledFormulaIds &&
            that.disabledFormulaIds.length > 0 &&
            that.disabledFormulaIds.indexOf(record.id) > -1,
        },
      }
    },
    initDictConfig() {},
    show() {
      this.selectedRowKeys = []
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      if (this.sectionSelectedRowKeys.length <= 0) {
        this.$message.warning('请选择标段！')
        return
      }
      if (!this.existProjectInfo) {
        this.projectInfo = this.selectionRows[0]
      }
      this.$emit('ok', { sectionInfo: this.sectionSelectionRows[0], projectInfo: this.projectInfo })
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    onSectionSelectChange(selectedRowKeys, selectionRows) {
      this.sectionSelectedRowKeys = selectedRowKeys
      this.sectionSelectionRows = selectionRows
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      var that = this
      that.selectedRowKeys = selectedRowKeys
      that.selectionRows = selectionRows
      that.sectionSelectedRowKeys = []
      that.sectionSelectionRows = []
      that.innerData = []
      that.loadSectionList(selectedRowKeys[0])
    },
    loadSectionList(projectId) {
      var that = this
      getAction(this.url.sectionList, { id: projectId })
        .then((res) => {
          if (res.success) {
            that.innerData = res.result
          } else {
            that.$message.warning(res.message)
          }
        })
        .finally(() => {
          that.confirmLoading = false
        })
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>